package com.keldian.myqq.tabhost;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.keldian.myqq.R;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

//动态
public class MyQQDynamic extends Activity implements View.OnClickListener {

    private Button btnTakePhoto;
    private ImageView picture;

    public static final int TAKE_PHOTO = 1;
    public static final int CROP_PHOTO = 2;

    private Uri imageUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab_item_dynamic);
        assignViews();

        btnTakePhoto.setOnClickListener(this);

    }

    private void assignViews() {
        btnTakePhoto = (Button) findViewById(R.id.btn_take_photo);
        picture = (ImageView) findViewById(R.id.picture);
    }


    @Override
    public void onClick(View v) {
        File outImageFile = new File(Environment.getExternalStorageDirectory(), "tempImage.jpg");
        try {
            if (outImageFile.exists()) {
                outImageFile.delete();
            }
            outImageFile.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        imageUri = Uri.fromFile(outImageFile);
        Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, TAKE_PHOTO);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode ,Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case TAKE_PHOTO:
                if (resultCode == RESULT_OK) {
                    Intent intent1 = new Intent("com.android.camera.action.CROP");
                    intent1.setDataAndType(imageUri, "image/*");
                    intent1.putExtra("scale", true);
                    intent1.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
                    startActivityForResult(intent1, CROP_PHOTO);
                }
                break;
            case CROP_PHOTO:
                if (resultCode == RESULT_OK) {
                    try {
                        Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver()
                                .openInputStream(imageUri));
                        picture.setImageBitmap(bitmap);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }

    }

}
